package reflection;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import sun.security.provider.MD5;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * User:bob.yang
 * Date 2015/10/16 0016
 */
public class TestHttpClient {

    public static void main(String []args) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost post = new HttpPost("http://mobile.mmbang.net/push/message/kangda");
        // 创建参数队列
        List<NameValuePair> formparams = new ArrayList<>();
        formparams.add(new BasicNameValuePair("appid", "test"));
        formparams.add(new BasicNameValuePair("openid", "0123456789adcdef"));
        formparams.add(new BasicNameValuePair("content", "[]"));
        formparams.add(new BasicNameValuePair("description", "康大测试"));
        String content = "appid"+ "test" + "content" + "[]" + "description" + "康大测试" + "openid" + "0123456789adcdef" +"secret";
        String key = DigestUtils.md5Hex(content);
        System.out.println("key:" + key);
        formparams.add(new BasicNameValuePair("sign", key));
        UrlEncodedFormEntity uefEntity;
        uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
        post.setEntity(uefEntity);
        System.out.println("executing request " + post.getURI());
        CloseableHttpResponse response = httpClient.execute(post);
        HttpEntity entity = response.getEntity();
        System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));
        response.close();
    }
}
